package com.thundersoft.beautifulmusic.ui;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.widget.ViewPager2;

import android.Manifest;
import android.app.AlertDialog;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.KeyEvent;
import android.widget.Toast;

import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;
import com.thundersoft.beautifulmusic.R;
import com.thundersoft.beautifulmusic.adapter.ViewPagerAdapter;
import com.thundersoft.beautifulmusic.other.SongService;
import com.thundersoft.beautifulmusic.util.LogUtil;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private LocalFragment mLocalFragment;
    private final String[] mPageName = {"本地音乐"};

    public static final String TAG = "MainActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // 申请权限，然后获得歌曲list
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE)
                != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 1);
        }

        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) {
            if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
                    != PackageManager.PERMISSION_GRANTED) {
                ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
            }
        }

        Log.d(TAG, "handleMessage: currentThread = " + Thread.currentThread().getName());
        // 启动服务
        Intent songServiceIntent = new Intent(this, SongService.class);
        startService(songServiceIntent);

        // init
        TabLayout mTabLayout = findViewById(R.id.tabLayout);
        ViewPager2 mViewPager = findViewById(R.id.viewPager);

        List<Fragment> mFragments = new ArrayList<>();
        Log.d(TAG, "onCreate");
        mLocalFragment = new LocalFragment();
        mFragments.add(mLocalFragment);
        mViewPager.setAdapter(new ViewPagerAdapter(this, mFragments));

        mViewPager.setOffscreenPageLimit(3);
        TabLayoutMediator mediator = new TabLayoutMediator(
                mTabLayout,
                mViewPager,
                (tab, position) -> tab.setText(mPageName[position]));
        mediator.attach();
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == 1) {
            if (grantResults[0] != PackageManager.PERMISSION_GRANTED) {
                Toast.makeText(this, "你没有权限", Toast.LENGTH_SHORT).show();
            }
        }
    }

    @Override
    protected void onDestroy() {
        Intent intent = new Intent(this, SongService.class);
        stopService(intent);
        super.onDestroy();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            LogUtil.d(TAG, "KEYCODE_BACK");
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            AlertDialog alertDialog = builder.setTitle(R.string.exit_title)
                    .setMessage(R.string.exit_message)
                    .setIcon(R.mipmap.dialog_exit)
                    .setPositiveButton("退出", (dialog, which) -> finish())
                    .setNegativeButton("不退出", (dialog, which) -> {
                        // Do nothing
                    })
                    .create();
            alertDialog.show();
        }
        return false;
    }

}